using FreeSwitch.EventSocket.Events.Channel;

namespace FreeSwitch.EventSocket.Events.Sip
{
	[EventName("PRESENCE")]
	public class Presence : SipEvent
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="Presence"/> class.
		/// </summary>
		public Presence()
		{
			ChannelState = new ChannelStateEvent();
			PresenceEventType = string.Empty;
			Rpid = string.Empty;
			Status = string.Empty;
			Login = string.Empty;
		}

		public string Login { get; set; }

		/// <summary>
		/// "Click to call", "Registered", "unavailable", "Active (%d waiting)", "Idle"
		/// </summary>
		public string Status { get; set; }

		public string Rpid { get; set; }

		/// <summary>
		/// Caller is only specified on state CS_ROUTING
		/// and not on CS_HANGUP
		/// </summary>
		public PartyInfo Caller { get; set; }

		/// <summary>
		/// presence, 
		/// </summary>
		public string PresenceEventType { get; set; }

		/// <summary>
		/// We *may* have channel state info
		/// </summary>
		public ChannelStateEvent ChannelState { get; set; }


		/*
        proto: sip
        login: sip%3Amod_sofia%40192.168.0.58%3A5070
        rpid: unknown
        from: gauffin%40gauffin.com
        status: Registered
         * */
		public override bool ParseCommand(string name, string value)
		{
			bool res = ChannelState.ParseCommand(name, value);
			switch (name)
			{
				case "status":
					Status = value;
					break;
				case "rpid":
					Rpid = value;
					break;
				case "login":
					Login = value;
					break;
				case "event_type":
					PresenceEventType = value;
					break;

				default:

					if (name.Length > 7 && name.Substring(0, 7) == "caller-")
					{
						if (Caller == null)
							Caller = new PartyInfo();
						return Caller.Parse(name.Substring(7), value);
					}

					return base.ParseCommand(name, value) || res;
			}
			return true;
		}

		public override string ToString()
		{
			return "Presence(" + Login + ", " + Status + ")." + ChannelState + "." + base.ToString();
		}
	}
}